package top.drinyeit.nongmao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.List;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.model.CategoryBean;

/**
 * Created by wu on 2018/1/9.
 */

public class ClassificationAdapter extends RecyclerView.Adapter<ClassificationAdapter.ViewHolder> {

    private Context context;
    private List<CategoryBean> list;

    public ClassificationAdapter(List<CategoryBean> list) {
        this.list = list;
    }

    public OnItemClickListener itemClickListener;

    public void setOnItemClickListener(ClassificationAdapter.OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public ClassificationAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View v = LayoutInflater.from(context).inflate(R.layout.item_home_icon, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ClassificationAdapter.ViewHolder holder, int position) {
        Glide.with(context).load(list.get(position).getCategory_img_url())
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .placeholder(R.drawable.ic_def)
                .dontTransform()
                .into(holder.img);
        holder.name.setText(list.get(position).getCategory_name());
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ImageView img;
        public TextView name;

        public ViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.img);
            name = itemView.findViewById(R.id.name);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, getAdapterPosition());
            }
        }
    }
}
